package si.dejan.picflip.views
{
	import com.adobe.webapis.flickr.PagedPhotoList;
	import com.adobe.webapis.flickr.Photo;
	import com.greensock.TweenMax;
	
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.system.Capabilities;
	
	import mx.controls.Image;
	import mx.controls.Label;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	import si.dejan.picflip.ApplicationFacade;
	import si.dejan.picflip.enum.Directions;
	import si.dejan.picflip.model.BackgroundProxy;
	import si.dejan.picflip.model.PhotoProxy;
	import si.dejan.picflip.model.SearchProxy;

	public class BackgroundMediator extends Mediator implements IMediator
	{
		public static const NAME:String = "BackgroundMediator";

		private var searchProxy:SearchProxy;
		private var bgProxy:BackgroundProxy;

		public function BackgroundMediator(viewComponent:Background)
		{
			super(NAME, viewComponent);

			searchProxy = facade.retrieveProxy(SearchProxy.NAME) as SearchProxy;
			bgProxy = facade.retrieveProxy(BackgroundProxy.NAME) as BackgroundProxy;
		}

		override public function listNotificationInterests():Array
		{
			return [SearchProxy.SEARCH,
					ApplicationFacade.IMAGE_CLICK,
					ApplicationFacade.IMAGE_DOUBLE_CLICK,
					ApplicationFacade.NEXT_SET,
					ApplicationFacade.PREV_SET];
		}

		override public function handleNotification(notification:INotification):void
		{
			switch(notification.getName())
			{
				case SearchProxy.SEARCH :
					ui.removeAllChildren();
					createPhotos(searchProxy.getNextSet());
				break;
				case ApplicationFacade.IMAGE_CLICK :
					ui.setChildIndex(notification.getBody() as DisplayObject, ui.numChildren - 1);
				break;
				case ApplicationFacade.NEXT_SET :
					moveAllChildren(Directions.LEFT);
					createPhotos(searchProxy.getNextSet());
				break;
				case ApplicationFacade.PREV_SET:
					moveAllChildren(Directions.RIGHT);
					createPhotos(searchProxy.getPreviousSet());
				break;
				case ApplicationFacade.IMAGE_DOUBLE_CLICK :
					trace("nič se ne rabi tu zgodit");
				break;
			}
		}

		private function createPhotos(photos:Array):void
		{
			trace("create photos", photos.length);
			for each(var photo:Photo in photos)
			{
				var imageComponent:ImageComponent = new ImageComponent();
				imageComponent.x = 0;
				imageComponent.y = 0;
				ui.addChild(imageComponent);
				
				if(!facade.hasProxy(photo.id + "Proxy"))
					facade.registerProxy(new PhotoProxy(photo.id + "Proxy", photo));
				facade.removeMediator(photo.id + "Mediator");
				facade.registerMediator(new ImageMediator(photo.id, imageComponent));
			}
		}

		private function moveAllChildren(direction:String):void
		{
			for each(var child:Object in ui.getChildren())
				if(child is ImageComponent)
				{
					switch(direction)
					{
						case Directions.LEFT :
							TweenMax.to(child, 1, {x:-300, onComplete:handleHideComplete, onCompleteParams:[child]});
						break;
						case Directions.RIGHT :
							TweenMax.to(child, 1, {x:Capabilities.screenResolutionX + 300, onComplete:handleHideComplete, onCompleteParams:[child]});
						break;
					}
				}
		}

		private function handleHideComplete(target:Object):void
		{
			ui.removeChild(target as DisplayObject);
		}

		/*
		private function loadBigPhoto(smallImage:Image):void
		{
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, handleBigImageLoadComplete);
			var newUrl:String = bgProxy.imageMap[smallImage].replace("_m", "_b");
			loader.load(new URLRequest(newUrl));
		}

		private function handleBigImageLoadComplete(event:Event):void
		{
			smallImage.source = event.target.content;
		}

		/*
		private function loadPhotos(photos:Array):void
		{
			for each(var photo_url:String in photos)
			{
				var loader:Loader = new Loader();
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE, handleImageLoadComplete);
				loader.load(new URLRequest(photo_url));
			}
		}

		private function handleImageLoadComplete(event:Event):void
		{
			var img:Image = new Image();
			img.source = event.target.content;

			bgProxy.imageMap[img] = event.target.url;
			ui.addChild(img);

			ui.movePhoto.target = img;
			ui.movePhoto.xTo = Math.abs(Math.random() * Capabilities.screenResolutionX - event.target.content.width);
			ui.movePhoto.yTo = Math.abs(Math.random() * Capabilities.screenResolutionY - event.target.content.height);
			ui.movePhoto.play();

			facade.registerMediator(new ImageMediator(img.uid, img));
		}
		*/
		private function get ui():Background
		{
			return viewComponent as Background;
		}
	}
}